<?php

class Weibo extends Controller {

	function Weibo()
	{		
		parent::__construct();
		include_once(APPPATH."/libraries/weibooauth.php");
		include_once(APPPATH."/libraries/weiboconfig.php");
		set_time_limit(60*10);
				
	}
	
	function hdcontent()
	{
		$sql = "select id,description from history where description LIKE  '%.com%' or description LIKE  '%.cn%' ";
		//$sql = "select id,description from history where id=17";
		$this->load->database();  
 		$query = $this->db->query($sql);
 		foreach ($query->result() as $row)
 		{
 			echo $row->id."<br/>";
 			$description = $row->description;
 			$description = str_replace('（','(',$description);
 			$description = str_replace('）',')',$description);
 			//echo $description;
 			preg_match_all("/[\[\(]ls.*?[\)\]]/i", $description, $matches, PREG_SET_ORDER);
 			foreach($matches as $match)
 			{
 				echo $match[0]."<br/>";
 				$description = str_replace($match[0],"",$description);
 			}
 			preg_match_all("/[\[\(]TodayinHistory.*?[\)\]]/i", $description, $matches, PREG_SET_ORDER);
 			foreach($matches as $match)
 			{
 				echo $match[0]."<br/>";
 				$description = str_replace($match[0],"",$description);
 			} 			

 			preg_match_all("/[\[\(]TodayonHistory.*?[\)\]]/i", $description, $matches, PREG_SET_ORDER);
 			foreach($matches as $match)
 			{
 				echo $match[0]."<br/>";
 				$description = str_replace($match[0],"",$description);
 			} 			

 			preg_match_all("/[\[\(][^([]*?\.com[\)\]]/i", $description, $matches, PREG_SET_ORDER);
 			foreach($matches as $match)
 			{
 				echo $match[0]."<br/>";
 				$description = str_replace($match[0],"",$description);
 			}
 			preg_match_all("/[\[\(][^([]*?\.cn[\)\]]/i", $description, $matches, PREG_SET_ORDER);
 			foreach($matches as $match)
 			{
 				echo $match[0]."<br/>";
 				$description = str_replace($match[0],"",$description);
 			}
 			 			
 			if(0)
 			{
				$data = array("description"=>$description);
				$this->db->update('history', $data, "id = ".$row->id);
 			} 			
 		}
	}
	function history()
	{
		/*
		 *
		 *  hjue 
		 $oauth_token = "9beb84d0f1c3d7063858f280fc0c1faa";
		$oauth_token_secret = "dd55059ac1f38b881d5a7b35067c9136"; 
		*/
		
		$oauth_token = "62d7c0bcc4bf7ba34c3e49631a2bb029";
		$oauth_token_secret = "b11953fd0bee78bdd7ce154421982846";
		
				
		$c = new WeiboClient( WB_AKEY , WB_SKEY ,$oauth_token,$oauth_token_secret  );
		
		$this->load->model("history_model");
		$this->load->model("history_img_model");
		$month = intval(date("m"));
		$day = intval(date("d"));
		$condition = "month='$month' and day='$day' and weiboid=0";
		//$condition = "id=2";
		$hlist = $this->history_model->getList($condition);
		$title = "";
		$description = "";
		$filename = "";
		$body = "";
		$id = 0;
		foreach($hlist as $info)
		{
			$id = $info->id;
			$title = $info->title;
			$description = $info->description;
			$condition = "hid='$id'";
			$img = $this->history_img_model->getInfo($condition);
			if($img->id>0)
			{
				$filename = $this->config->item("base_url")."/history/pic/$month/$day/".$img->filename;
				//$filename = "D:/program/workspace/my-code-snippets/src"."/history/pic/$month/$day/".$img->filename;
				
			}
			if($title=="" and $description=="") return ;
			$month = $info->month;
			$day = $info->day;
			$url = "http://history.ttsgs.com/index.php/$month/$day/$id.html";
			break;	
			
		}
		
		$body = mb_substr("#历史上的今天# ".$title . "\n" .$description,0, 130,"utf8");
		$body = $body . "  $url";
		echo $body;
		if($filename!="")
		{
			echo $filename."<br/>";
			$rr = $c ->upload( $body, $filename );
		}else
			$rr = $c ->update( $body );
		if($rr['error_code']>0)
			print_r($rr);
		else
		{
			$weiboid = $rr["id"];
			//$weiboid =  1;
			if($weiboid>0)
			{
				$o = new stdClass();
				$o->id = $id;
				$o->weiboid = $weiboid;
				$this->history_model->update($o);
			}
			
		}
			echo "OK";
	
	}
	
	
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */